package com.wlc.service.impl;

import com.wlc.mapper.EmployeeStatsMapper;
import com.wlc.pojo.EmployeeStats;
import com.wlc.service.EmployeeStatsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class EmployeeStatsServiceImpl implements EmployeeStatsService {

    @Autowired
    private EmployeeStatsMapper employeeStatsMapper;

    @Override
    public List<EmployeeStats> getDeptEmployeeStats() {
        log.info("查询部门员工统计");
        return employeeStatsMapper.selectDeptEmployeeStats();
    }

    @Override
    public List<EmployeeStats> getGenderStats() {
        log.info("查询性别统计");
        return employeeStatsMapper.selectGenderStats();
    }

    @Override
    public List<EmployeeStats> getJobLevelStats() {
        log.info("查询职级统计");
        return employeeStatsMapper.selectJobLevelStats();
    }

    @Override
    public List<EmployeeStats> getWorkStateStats() {
        log.info("查询在职状态统计");
        return employeeStatsMapper.selectWorkStateStats();
    }

    @Override
    public List<EmployeeStats> getMonthlyHireLeaveStats(String year) {
        log.info("查询月度入职离职统计: year={}", year);
        if (year == null || year.trim().isEmpty()) {
            year = String.valueOf(java.time.Year.now().getValue());
        }
        return employeeStatsMapper.selectMonthlyHireLeaveStats(year);
    }

    @Override
    public EmployeeStats getEmployeeSummary(String year, String month) {
        log.info("查询员工汇总统计: year={}, month={}", year, month);
        if (year == null || year.trim().isEmpty()) {
            year = String.valueOf(java.time.Year.now().getValue());
        }
        if (month == null || month.trim().isEmpty()) {
            month = String.format("%02d", java.time.LocalDate.now().getMonthValue());
        }
        return employeeStatsMapper.selectEmployeeSummary(year, month);
    }

    @Override
    public Object getAllEmployeeStats(String year, String month) {
        log.info("查询所有员工统计数据: year={}, month={}", year, month);

        if (year == null || year.trim().isEmpty()) {
            year = String.valueOf(java.time.Year.now().getValue());
        }
        if (month == null || month.trim().isEmpty()) {
            month = String.format("%02d", java.time.LocalDate.now().getMonthValue());
        }

        Map<String, Object> result = new HashMap<>();
        result.put("deptStats", getDeptEmployeeStats());
        result.put("genderStats", getGenderStats());
        result.put("jobLevelStats", getJobLevelStats());
        result.put("workStateStats", getWorkStateStats());
        result.put("monthlyTrend", getMonthlyHireLeaveStats(year));
        result.put("summary", getEmployeeSummary(year, month));

        return result;
    }
}